//
//  CSSStatusOriginalView.swift
//  weibo28
//
//  Created by chushushan on 16/5/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit


//子控件之间的间距
let StatusTableViewCellMargin: CGFloat = 10

class CSSStatusOriginalView: UIView {
    
    //记录当前视图的底部约束
    var originalViewBottom: Constraint?
    
    //设置数据
    var statusViewModel: CSSStatusViewModel? {
        didSet {
        
            if let url = statusViewModel?.status?.user?.profile_image_url {
            userImageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            contentLabel.text = statusViewModel?.status?.text
          //时间和来源赋值
            timeLabel.text = statusViewModel?.timeContent
            
            sourceLabel.text = statusViewModel?.sourceContent
            
            //卸载约束
            originalViewBottom?.uninstall()
            
            //判断原创微博是否有配图
            if let picUrls = statusViewModel?.status?.pic_urls where picUrls.count > 0 {
            //判断有图片
                pictureView.hidden = false
            //设置数据
                pictureView.picUrls = picUrls
            
            //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.originalViewBottom = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                })
                
            
            } else {
            //没有配图
                pictureView.hidden = true
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBottom = make.bottom.equalTo(contentLabel).offset(StatusTableViewCellMargin).constraint
                })
               
            }
            
            //  设置会员等级图片
            mbrankImageView.image = statusViewModel?.mbrankImage
            
            // 设置认证类型等级图片
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
        }
    
    }
    
    

    //懒加载控件
    //用户头像
    private lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证类型等级
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //昵称
    private lazy var screenNameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
    //会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 12)
    //来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 12)
    
    //微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
            label.numberOfLines = 0
            return label
        }()
    //配图
    private lazy var pictureView: CSSStatusPictureView = {
        let view = CSSStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    
    }()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        backgroundColor = UIColor.purpleColor()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        backgroundColor = UIColor.whiteColor()
        
    //添加控件
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        
        //添加配图
        addSubview(pictureView)
        
        //设置约束
        userImageView.snp_makeConstraints { (make) -> Void in
            
            make.top.left.equalTo(self).offset(StatusTableViewCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(userImageView.snp_trailing)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userImageView)
            make.left.equalTo(userImageView.snp_right).offset(StatusTableViewCellMargin)
        }
        
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(screenNameLabel)
            make.left.equalTo(screenNameLabel.snp_right).offset(StatusTableViewCellMargin)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(userImageView.snp_bottom)
            make.left.equalTo(userImageView.snp_right).offset(StatusTableViewCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(StatusTableViewCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
           make.top.equalTo(userImageView.snp_bottom).offset(StatusTableViewCellMargin)
            make.left.equalTo(userImageView)
            make.right.equalTo(self).offset(-StatusTableViewCellMargin)
//            make.height.equalTo(80)
        }
        
        //配图的约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
            make.left.equalTo(contentLabel)
            
        }
        
        
        
        //当前视图加一个底部约束
        self.snp_makeConstraints { (make) -> Void in
            self.originalViewBottom = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
            
        }
        
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


}
